package fr.uhp.mpr.bayesnetwork;

import java.util.ArrayList;

import fr.uhp.mpr.data.DataExplore;
import fr.uhp.mpr.data.DataMeter;
import fr.uhp.mpr.data.Intervalle;
import fr.uhp.mpr.data.Jour;
import fr.uhp.mpr.tools.BayesRule;
import fr.uhp.mpr.tools.Proba;
import fr.uhp.mpr.tools.Table;

/**
 * NetworkTables permet de construire les tables de probabilites
 * de l'ensemble du reseau bayesien a l'aide de la classe Table.
 * @see Table
 */
public class NetworkTables {
        
        private Proba[] temperatureTable;
        private Proba[] windTable;
        private Proba[] rain;
        private Proba[] humidityTable;
        private Proba[][] pressureTable;
        private Proba[][] rainSachanttemperatureTable;
        private Proba[][] pressionSachantPluie;
        private Proba[][] humiditeSachantPluie;
        private Proba[][] ventSachantPluie;
        private Proba[][][][] rainTable;
        
        public static double confiance;
        
        private static NetworkTables instance = null;
        private Table table = null;
        
        private NetworkTables() {
                table = new Table();
                DataExplore.getInstance();
                
                rain = table.buildTable1D(
                        DataMeter.getIntervalle(DataMeter.iname.RAIN),
                        DataExplore.getData(DataMeter.iname.RAIN)
                        );
                
                temperatureTable = table.buildTable1D(
                                DataMeter.getIntervalle(DataMeter.iname.TEMPERATURE),
                                DataExplore.getData(DataMeter.iname.TEMPERATURE)
                                );
                
                windTable = table.buildTable1D(
                                DataMeter.getIntervalle(DataMeter.iname.WIND),
                                DataExplore.getData(DataMeter.iname.WIND)
                                );
                
                humidityTable = table.buildTable1D(
                                DataMeter.getIntervalle(DataMeter.iname.HUMIDITY),
                                DataExplore.getData(DataMeter.iname.HUMIDITY)
                                );
                
                pressureTable = table.buildTable2D(
                                DataMeter.getIntervalle(DataMeter.iname.TEMPERATURE), 
                                DataMeter.getIntervalle(DataMeter.iname.PRESSURE),
                                DataExplore.getData(DataMeter.iname.TEMPERATURE),
                                DataExplore.getData(DataMeter.iname.PRESSURE)
                                );
                
                rainTable = table.buildTable4D(
                                DataMeter.getIntervalle(DataMeter.iname.HUMIDITY),
                                DataMeter.getIntervalle(DataMeter.iname.PRESSURE),
                                DataMeter.getIntervalle(DataMeter.iname.WIND),
                                DataMeter.getIntervalle(DataMeter.iname.RAIN),
                                DataExplore.getData(DataMeter.iname.HUMIDITY),
                                DataExplore.getData(DataMeter.iname.PRESSURE),
                                DataExplore.getData(DataMeter.iname.WIND),
                                DataExplore.getData(DataMeter.iname.RAIN)
                                );
                
                rainSachanttemperatureTable = table.buildTable2D(
                        DataMeter.getIntervalle(DataMeter.iname.TEMPERATURE),
                        DataMeter.getIntervalle(DataMeter.iname.RAIN),
                        DataExplore.getData(DataMeter.iname.TEMPERATURE),
                        DataExplore.getData(DataMeter.iname.RAIN)
                        );
                
                
                pressionSachantPluie = table.buildTable2D(
                        DataMeter.getIntervalle(DataMeter.iname.PRESSURE),
                        DataMeter.getIntervalle(DataMeter.iname.RAIN),
                        DataExplore.getData(DataMeter.iname.PRESSURE),
                        DataExplore.getData(DataMeter.iname.RAIN)
                        );
                
                humiditeSachantPluie = table.buildTable2D(
                        DataMeter.getIntervalle(DataMeter.iname.HUMIDITY),
                        DataMeter.getIntervalle(DataMeter.iname.RAIN),
                        DataExplore.getData(DataMeter.iname.HUMIDITY),
                        DataExplore.getData(DataMeter.iname.RAIN)
                        );
                
                ventSachantPluie = table.buildTable2D(
                        DataMeter.getIntervalle(DataMeter.iname.WIND),
                        DataMeter.getIntervalle(DataMeter.iname.RAIN),
                        DataExplore.getData(DataMeter.iname.WIND),
                        DataExplore.getData(DataMeter.iname.RAIN)
                        );
                
                
        }

        public static NetworkTables getInstance() {
                if (instance == null)
                        instance = new NetworkTables();

                return instance;
        }

        public Proba[] getTemperatureTable() {
                return temperatureTable;
        }

        public Proba[] getWindTable() {
                return windTable;
        }

        public Proba[][] getRainSachanttemperatureTable() {
			return rainSachanttemperatureTable;
		}

		public Proba[] getHumidityTable() {
                return humidityTable;
        }

        public Proba[][] getPressureTable() {
                return pressureTable;
        }

        public Proba[][][][] getRainTable() {
                return rainTable;
        }
        
        private Proba[][] getPressionSachantPluie() {
			// TODO Auto-generated method stub
			return pressionSachantPluie;
		}

		public double testPerformance() {
                ArrayList<Jour> liste = DataMeter.importData();
                int count = 0;
                
                for(Jour j : liste) {
                	 if (j.getPluie().compare(BayesRule.getIntervallePluieSachant(j.getHumidite(), j.getPression(), j.getVent()))){
                        count++;
                        //System.out.println(BayesRule.getTemperatureGivenPressure(j.getPression()).toString());
                        //System.out.println(j.getPluie() + " " + BayesRule.getIntervallePluieSachant(j.getHumidite(), j.getPression(), j.getVent()));
                        }
                }
                
                return (double)count/liste.size();
        }

		public Proba[][] getHumiditeSachantPluie() {
			return humiditeSachantPluie;
		}

		public Proba[][] getVentSachantPluie() {
			return ventSachantPluie;
		}
        
        
}
